import { getBaseUrl } from '@/services/urlconst';
import { ManageCommonAPI } from '@/pages/type';
import { useRequest } from 'umi';
import { getLoadMapList, getTenantID, requestFunComponFiex } from '@/utils';
import { ILoadMapTypeAll } from '@/common';
import { useMemo } from 'react';
import { AssetMaterialManage } from '../mconfig/ManageTabSwitch';
import { getDHSearchList } from '../mconfig/UpdateDHSearchColumns';

const baseUrl = getBaseUrl();
// const baseUrl = '/wenchuang';

/**
 * 资产列表
 * @param curRes
 */
const applyStorageService = (curRes: Record<string, any>) => {
  /**  需要获取的资产类别数据字典 */
  const loadMdicIds: string[] = [`${getTenantID()}${ILoadMapTypeAll.ASSET_TYPE}`];

  /** 获取数据字典loadMdicMap */
  const loadMdicMapObj: Record<string, any> = getLoadMapList(loadMdicIds);

  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = AssetMaterialManage(curRes);

  /** 获取权限参数 */
  // const queryObj: Record<string, any> = materialSwitchTable(curRes);

  /** 获取列表数据  */
  const { loading, run, refresh, tableProps, data, params } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        ...queryTypeObj,
        current: 1,
        pageSize: 10,
        sort: 'createTime',
        order: 'desc',
        state: `${getTenantID()}${ILoadMapTypeAll.STORAGE_INSPECTION_STATUS}1003`,
        assetState: `${getTenantID()}${ILoadMapTypeAll.MATERIEL_STATUS}1004`, // 查询待入库的
        ...opt,
      };

      const requestURL = `${baseUrl}/assetApplyStorage/dataGrid/${queryTypeObj.params}`;
      return requestFunComponFiex('POST', requestURL, requestBody);
    },
    {
      manual: false, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /**  获取管理也数据  */
  const getManageData = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };
    run({ ...request });
  };

  /** 搜索表单 */
  const searchList: any = getDHSearchList(loadMdicMapObj);

  /** 页面列表 */
  const returnTableProps = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
      params: params?.[0],
    };
  }, [data, tableProps]);

  return {
    getManageData,
    refreshManage: refresh,
    loading,
    searchList,
    dataList: data?.rows,
    loadMdicMapObj,
    ...returnTableProps,
  };
};

export { applyStorageService };
